package com.example.crmdemo.Crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.crmdemo.Crm.entity.Area;
import com.example.crmdemo.Crm.entity.Customer;
import com.example.crmdemo.Crm.service.AreaService;
import com.example.crmdemo.Crm.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private AreaService areaService;

    // 添加客户
    @PostMapping("/add")
    public boolean addCustomer(@RequestBody Customer customer) {
        return customerService.save(customer);
    }

    // 删除客户
    @DeleteMapping("/delete/{id}")
    public boolean deleteCustomer(@PathVariable Long id) {
        return customerService.removeById(id);
    }

    // 修改客户信息
    @PutMapping("/update")
    public boolean updateCustomer(@RequestBody Customer customer) {
        return customerService.updateById(customer);
    }

    // 按客户名查询客户
    @GetMapping("/get/{name}")
    public List<Customer> getCustomerByName(@PathVariable String name) {
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("c_name", name);
        return customerService.list(queryWrapper);
    }

    // 获取所有客户信息
    @GetMapping("/getall")
    public List<Customer> getAllCustomers() {
        return customerService.list();
    }

    // 获取所有区域信息
    @GetMapping("/areas")
    public List<Area> getAllAreas() {
        return areaService.list();
    }
}
